#!/usr/bin/env python
# -*- coding: utf-8 -*-


from mock.user_mgr import UserMgr
from mock.project_mgr import ProjectMgr
from mock.task_mgr import TaskMgr
from mock.device_mgr import DeviceMgr
from mock.report_mgr import ReportMgr
from mock.template_mgr import TemplateMgr
from mock.config_mgr import ConfigMgr
import hashlib


class Control:
    def __init__(self, app, session):
        self.app = app
        self.session = session
        self.user_mgr = UserMgr(app, session)
        self.project_mgr = ProjectMgr(app, session)
        self.task_mgr = TaskMgr(app, session)
        self.device_mgr = DeviceMgr(app, session)
        self.report_mgr = ReportMgr(app, session)
        self.template_mgr = TemplateMgr(app, session)
        self.config_mgr = ConfigMgr(app, session)

    def destroy(self):
        if self.config_mgr:
            self.config_mgr.destroy()
            self.config_mgr = None
        if self.template_mgr:
            self.template_mgr.destroy()
            self.template_mgr = None
        if self.report_mgr:
            self.report_mgr.destroy()
            self.report_mgr = None
        if self.device_mgr:
            self.device_mgr.destroy()
            self.device_mgr = None
        if self.task_mgr:
            self.task_mgr.destroy()
            self.task_mgr = None
        if self.project_mgr:
            self.project_mgr.destroy()
            self.project_mgr = None
        if self.user_mgr:
            self.user_mgr.destroy()
            self.user_mgr = None
        self.session = None
        self.app = None 


    def __del__(self):
        self.destroy()
    

    def connect(self, request):
        self.user_mgr.connect(request)


    def disconnect(self, request):
        self.user_mgr.disconnect(request)


    def login(self, username: str, password: str, remember = False):
        user = None
        if username in self.user_mgr.users and self.user_mgr.users[username]['password'] == hashlib.md5(password.encode()).hexdigest():
            user = self.user_mgr.users[username]
        if user:
            # 登录成功，设置会话
            self.session['username'] = username
            self.session['name'] = user['name']
            self.session['role'] = user['role']
            # 如果选择了"记住我"，设置会话过期时间为30天
            if remember:
                self.session.permanent = True
                self.app.permanent_session_lifetime = timedelta(days=30)
            return True
        return False


    def logout(self):
        self.session.clear()
        return True


    def call_project_api(self, api:str, args: dict):
        match api:
            case 'get_projects':
                return self.project_mgr.get_projects(**args)
            case 'get_project_detail':
                return self.project_mgr.get_project_detail(**args)
            case 'open_project':
                return self.project_mgr.open_project(**args)
            case 'rename_project':
                return self.project_mgr.rename_project(**args)
            case 'update_project':
                return self.project_mgr.update_project(**args)
            case 'run_project':
                return self.project_mgr.run_project(**args)
            case 'new_project':
                return self.project_mgr.new_project(**args)
            case 'delete_project':
                return self.project_mgr.delete_project(**args)
            case 'import_projects':
                return self.project_mgr.import_projects(**args)
            case 'export_projects':
                return self.project_mgr.export_projects(**args)
            case 'get_test_cases':
                return self.project_mgr.get_test_cases(**args)
            # case 'new_test_case':
            #     return self.project_mgr.new_test_case(**args)
            # case 'open_test_case':
            #     return self.project_mgr.open_test_case(**args)
            # case 'run_test_case':
            #     return self.project_mgr.run_test_case(**args)
            # case 'run_test_step':
            #     return self.project_mgr.run_test_step(**args)
            # case 'update_test_case':
            #     return self.project_mgr.update_test_case(**args)
            # case 'delete_test_cases':
            #     return self.project_mgr.delete_test_cases(**args)
            # case 'import_test_cases':
            #     return self.project_mgr.import_test_cases(**args)
            # case 'export_test_cases':
            #     return self.project_mgr.export_test_cases(**args)
            case 'get_test_tasks':
                return self.project_mgr.get_test_tasks(**args)
            # case 'new_test_task':
            #     return self.project_mgr.new_test_task(**args)
            case _:
                return {'code': 500, 'content': {'message': f'project api: {api} not supported in mock mode'}}
        

    def call_task_api(self, api:str, args: dict):
        match api:
            case 'get_tasks':
                return self.task_mgr.get_tasks(**args)
            case 'open_task':
                return self.task_mgr.open_task(**args)
            case 'update_task':
                return self.task_mgr.update_task(**args)
            case 'sched_task':
                return self.task_mgr.sched_task(**args)
            case 'new_task':
                return self.task_mgr.new_task(**args)
            case 'delete_task':
                return self.task_mgr.delete_task(**args)
            case _:
                return {'code': 500, 'content': {'message': f'task api: {api} not supported in mock mode'}}


    def call_device_api(self, api:str, args: dict):
        match api:
            case 'get_devices':
                return self.device_mgr.get_devices(**args)
            case 'open_device':
                return self.device_mgr.open_device(**args)
            case _:
                return {'code': 500, 'content': {'message': f'device api: {api} not supported in mock mode'}}
        

    def call_report_api(self, api:str, args: dict):
        match api:
            case 'get_reports':
                return self.report_mgr.get_reports(**args)
            case 'open_report':
                return self.report_mgr.open_report(**args)
            case 'export_reports':
                return self.report_mgr.export_reports(**args)
            case 'delete_reports':
                return self.report_mgr.delete_reports(**args)
            case _:
                return {'code': 500, 'content': {'message': f'report api: {api} not supported in mock mode'}}
        

    def call_template_api(self, api:str, args: dict):
        match api:
            case 'get_templates':
                return self.template_mgr.get_templates(**args)
            case _:
                return {'code': 500, 'content': {'message': f'template api: {api} not supported in mock mode'}}
        

    def call_config_api(self, api:str, args: dict):
        pass